<template>
  <el-dialog
    class="pick-dialog"
    :title="'签署合同'"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    append-to-body
    width="620px"
  >
    <div class="form-box">
      <el-form
        :model="ruleForm"
        ref="ruleForm"
        label-width="auto"
        :rules="rules"
        class="demo-ruleForm"
      >
        <el-form-item label="认证名称:" prop="realName">
          <el-input
          :disabled="shopInfo&&shopInfo.fddCorpName"
            v-model="ruleForm.realName"
            placeholder="请输入认证名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="社会信用代码/身份证号:" prop="idCard">
          <el-input
          :disabled="shopInfo&&shopInfo.fddCorpIdentNo"
            v-model="ruleForm.idCard"
            placeholder="请输入社会信用代码/身份证号"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号:" prop="phonenumber">
          <el-input
          :disabled="shopInfo&&shopInfo.authPhone"
            v-model="ruleForm.phonenumber"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="联系地址:" prop="adress">
          <el-input
          :disabled="userInfo.adress!=''&&userInfo.adress!=null&&userInfo.adress.trim()!=''"
            v-model="ruleForm.adress"
            placeholder="请输入联系地址"
          ></el-input>
        </el-form-item> -->
        
      
      </el-form>
      <div v-if="showSureBtn" style="text-align: center">
        <el-button @click="showSureBtnFn" type="text">完成签署，点此刷新</el-button>
      </div>
      <div class="center" v-else>
        <el-button @click="close('ruleForm')">取消</el-button>
        <el-button
          type="primary"
          @click="submitForm('ruleForm')"
          :loading="loading"
          >去签合同</el-button
        >
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { mapGetters } from "vuex";
import { getInfo } from "@/api/login";
import fadada from '@/api/B2Cmanage/fadada'
import { getShopDetail } from '@/api/system/shopManage'
export default {
  name: "payEdit",
 
  data() {
    var idCardRule = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入身份证号"));
      } else if (!this.idCardReg.test(value)) {
        callback(new Error("请输入正确的身份证号"));
      } else {
        callback();
      }
    };
    return {
      dialogVisible: false,
      loading: false,
      ruleForm: {
        adress: "",
        phonenumber: "",
        idCard: "",
        realName: "",
        tenantId:''
      },
      rules: {
        phonenumber: [
          { required: true, message: "请输入", trigger: "blur" },
        ],
        adress:[
        { required: true, message: "请输入", trigger: "blur" },
        ],
        idCard:[
        { required: true, message: "请输入", trigger: "blur" },
        // { validator: idCardRule, trigger: "blur" },
        ],
        realName:[
        { required: true, message: "请输入", trigger: "blur" },
        ]
      },
      from: "", //来源
      showSureBtn:false
    };
  },
  computed: {
    ...mapGetters(["userInfo", "shopInfo"]),
  },
  created() {},
  methods: {
    async open() {
      this.loading = false;
      this.showSureBtn = false;
      const user = this.userInfo
      this.ruleForm = {
          adress: user.adress?user.adress.trim():'',
          phonenumber: this.shopInfo.authPhone||'',
          idCard: this.shopInfo.fddCorpIdentNo,
          realName: this.shopInfo.fddCorpName?this.shopInfo.fddCorpName.trim():'',
          tenantId:this.shopInfo.tenantId
        };
      if(this.shopInfo.signContractState==2||this.shopInfo.signContractState==3){
        return this.signFdd()
      }
      this.dialogVisible = true;
    },
    async getInfo(){
      let {user} = await getInfo()
      return user
    },
    submitForm(formName) {
      this.showSureBtn = false
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          
          this.signFdd()
        } else {
          //console.log("error submit!!");
          return false;
        }
      });
    },
    signFdd(){
      fadada.fddTenantContractSign(this.ruleForm).then(res=>{
            // this.dialogVisible = false
            window.open(res.data.jfUrl)
            this.loading = false
            this.showSureBtn = true
          }).catch(err=>{
            this.showSureBtn = false
            this.loading = false
          })
    },
    // 刷新店铺信息
    showSureBtnFn(){
      // 直接查询店铺信息
      getShopDetail(this.shopInfo.tenantId).then(res => {
        if (res.code == 200) {
          if(res.data.signContractState&&res.data.signContractState==3){
            this.close()
            this.$store.dispatch("getShopInfo");
            this.$emit('next')
          }else{
            this.$message.error('还未完成合同签署！')
          }
        }
      });
    },
    close(formName) {
      this.dialogVisible = false;
    },
  },
};
</script>
<style scoped lang="scss">
.demo-ruleForm {
  padding-right: 20px;
}

.center {
  text-align: center;
}



.pick-dialog >>> .el-input--medium input {
  text-align: left;
}
</style>
